package com.hbnu.mysql.Flower;

import com.hbnu.mysql.Cart.CartService;
import com.hbnu.mysql.Cart.OrderService;
import com.hbnu.mysql.User.UserCenter;

import java.util.List;
import java.util.Scanner;
/*
 * @author zouting
 * @Date 2025-11-05-20:58
 *
 */public class FlowerShop {
     private final String loginUser;          // 当前登录用户
     private final FlowerDAO flowerDAO = new FlowerDAO();
     private final Scanner scanner = new Scanner(System.in);
     private final UserCenter userCenter;
     private final CartService cartService;
     private final OrderService orderService;

     public FlowerShop(String loginUser) {
          this.loginUser = loginUser;
          this.cartService = new CartService(loginUser);
          this.userCenter = new UserCenter(loginUser);
          this.orderService = new OrderService(loginUser);
     }

     public void enter() {
          System.out.println("========== 欢迎 " + loginUser + " 来到鲜花商城 ==========");
          while (true) {
               menu();
               String opt = scanner.nextLine().trim();
               switch (opt) {
                    case "1":
                         listFlower();
                         break;
                    case "2":
                         listByCategory();
                         break;
                    case "3":
                         addCart();
                         break;
                    case "4":
                         cartManagement();
                         break;
                    case "5":
                         userCenter.showMenu();
                         break;
                    case "6":
                         orderManagement();
                         break;
                    case "7":
                         System.out.println("已退出商城，欢迎下次再来！");
                         return;
                    default:
                         System.out.println("输入错误，请重选！");
               }
          }
     }

     private void menu() {
          System.out.println("\n========= 菜单 ==========");
          System.out.println("1. 查看所有鲜花");
          System.out.println("2. 按分类浏览");
          System.out.println("3. 添加到购物车");
          System.out.println("4. 购物车管理");
          System.out.println("5. 个人中心");
          System.out.println("6. 订单管理");
          System.out.println("7. 退出商城");
          System.out.print("请选择：");
     }

     /*
     * 1.查看所有鲜花
     * */
     private void listFlower() {
          try {
               List<Flower> list = flowerDAO.listAll();
               System.out.println("\n编号\t名称\t花语\t价格\t库存\t分类");
               list.forEach(flower -> {
                    System.out.println(flower.getFid() + "\t" + flower.getFname() + "\t" + flower.getLanguage() + "\t" +"￥" + flower.getPrice() + "\t" + flower.getStock() + "\t" + flower.getCategory());
               });
          } catch (Exception e) {
               System.out.println("查询鲜花失败：" + e.getMessage());
          }
     }

     /*
     * 2.按分类浏览鲜花
     * */
     private void listByCategory() {
           try {
                List<String> categories = flowerDAO.getAllCategories();
                if (categories.isEmpty()){
                     System.out.println("暂时没有分类的信息！");
                     return;
                }
                System.out.println("\n======== 鲜花分类 =============");
                for (int i = 0; i < categories.size(); i++) {
                     System.out.println((i + 1) + ". " + categories.get(i));
                }
                System.out.print("请选择分类编号：");
                try {
                     int categoryIndex = Integer.parseInt(scanner.nextLine().trim()) - 1;
                     if (categoryIndex >= 0 && categoryIndex < categories.size()) {
                          String selectedCategory = categories.get(categoryIndex);
                          List<Flower> flowers = flowerDAO.listByCategory(selectedCategory);
                          System.out.println("\n分类：" + selectedCategory+"鲜花列表：");
                          System.out.println("\n编号\t名称\t花语\t价格\t库存");
                          flowers.forEach(flower -> {
                               System.out.println(flower.getFid() + "\t" + flower.getFname() + "\t" + flower.getLanguage() + "\t" +"￥" + flower.getPrice() + "\t" + flower.getStock());
                          });
                     }else{
                          System.out.println("输入编号无效");
                     }
                 }catch (Exception e){
                     System.out.println("请输入有效的编号");
                }
           }catch (Exception e){
                System.out.println("查询分类失败：" + e.getMessage());
           }
     }

     /*
     * 3.添加到购物车*/
     private void addCart() {
          System.out.print("请输入要购买的鲜花编号：");
          String fid = scanner.nextLine().trim();
          //先检查鲜花是否存在
          try {
               Flower flower = flowerDAO.getFlowersById(fid);
               if (flower == null) {
                    System.out.println("鲜花不存在！");
                    return;
               }
               System.out.print("请输入要购买的数量：");
               try{
                    int quantity = Integer.parseInt(scanner.nextLine().trim());
                    if (quantity <= 0) {
                         System.out.println("数量必须大于0！");
                         return;
                    }
                    //检查库存
                    if (!flowerDAO.checkStock(fid, quantity)) {
                         System.out.println("库存不足！当前库存："+flower.getStock());
                         return;
                    }
                    //添加到购物车
                    for(int i = 0; i < quantity; i++){
                         flowerDAO.addToCart(loginUser, fid);
                    }
                    System.out.println("添加成功！已经添加"+quantity+"个"+flower.getFname()+"到购物车！");
               }catch (Exception e){
                    System.out.println("请输入有效的数量！");
               }
          }catch (Exception e){
               System.out.println("添加鲜花失败：" + e.getMessage());
          }
     }

     /*
     * 4.购物车管理
     * */
     private void cartManagement() {
          while(true){
               System.out.println("\n========== 购物车管理 ==========");
               System.out.println("1. 查看购物车");
               System.out.println("2. 修改商品数量");
               System.out.println("3. 删除商品");
               System.out.println("4. 清空购物车");
               System.out.println("5. 结算订单");
               System.out.println("6. 返回上一级");
               System.out.print("请选择：");

               String choice = scanner.nextLine().trim();
               switch (choice){
                    case "1":
                         cartService.viewCart();
                         break;
                    case "2":
                         cartService.updateCartItem();
                         break;
                    case "3":
                         System.out.print("请输入要删除的购物车项目ID: ");
                         String cid = scanner.nextLine().trim();
                         cartService.deleteCartItem(cid);
                         break;
                    case "4":
                         cartService.clearCart();
                         break;
                    case "5":
                         cartService.checkout();
                         break;
                    case "6":
                         return;
                    default:
                         System.out.println("无效选择");
               }
          }
     }

     /**
      * 订单管理菜单
      */
     private void orderManagement() {
          while (true) {
               System.out.println("\n========== 订单管理 ==========");
               System.out.println("1. 查看我的订单");
               System.out.println("2. 查看订单详情");
               System.out.println("3. 创建新订单");
               System.out.println("4. 订单支付");
               System.out.println("5. 返回上一级");
               System.out.print("请选择：");

               String choice = scanner.nextLine().trim();
               switch (choice) {
                    case "1":
                         orderService.viewUserOrders();
                         break;
                    case "2":
                         orderService.viewOrderDetail();
                         break;
                    case "3":
                         orderService.createOrder();
                         break;
                    case "4":  // 处理支付
                         System.out.print("请输入要支付的订单号: ");
                         String orderId = scanner.nextLine().trim();
                         orderService.payOrder(orderId);
                         break;
                    case "5":
                         return;
                    default:
                         System.out.println("无效选择");
               }
          }
     }
}

